#include <dspSimulator/command/saveAndrestore.h>
#include <dspSimulator/dspSimulator/dspSimulator.h>

std::shared_ptr<SaveAndRestoreCommand> SaveAndRestoreCommand::get(Simulator *dspSimulator) {
    static std::shared_ptr<SaveAndRestoreCommand> saveAndRestore;
    if (saveAndRestore == nullptr) {
        saveAndRestore = std::make_shared<SaveAndRestoreCommand>(dspSimulator);
    }
    return saveAndRestore;
}

void SaveAndRestoreCommand::execute(std::string command) {

    auto dspSimulator = dynamic_cast<DSPSimulator *>(m_simulator);
    if (dspSimulator == nullptr) {
        fatalMacro("该模拟器对象不为dsp模拟器对象!");
    }
    auto targetMachine = dspSimulator->m_targetMachine;
    auto dspMachine = std::dynamic_pointer_cast<DspMachine>(targetMachine);
    if (dspMachine == nullptr) {
        fatalMacro("该目标机对象不为dsp目标机对象!");
    }

    if (command == "save") {
        m_snapshots.push_back(dspSimulator->createSnapshot());
    } else if (command == "restore") {
        if (m_snapshots.empty()) {
            warningMacro("当前无可用的快照信息!!!");
        }
        dspSimulator->restore(m_snapshots.back());
        m_snapshots.pop_back();
    }
}